package com.Jin.controller;

import com.Jin.pojo.User;
import com.Jin.service.CarPlateService;
import com.Jin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserControl {

    @Autowired
    private UserService userService;    //user业务

    @Autowired
    private CarPlateService carPlateService;    //车牌业务

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody User users, HttpServletRequest req){
        String account = users.getAccount();
        String password = users.getPassword();
        User user = userService.login(account,password);

        HttpSession session = req.getSession();
        System.out.println("login: "+user);

        //与数据库数据进行校验
        Integer code=0;
        String msg = "";
        boolean flag = false;   //判断用户身份
        if (user!=null){
            session.setAttribute("user",user);
            code = Code.SELECT_OK;
            msg = "登录成功";
            if ("管理员".equals(user.getIdentify())){
                flag = true;
            }
        }else {
            code = Code.SELECT_ERR;
            msg = "登录失败";
        }
        return new Result(code,msg,flag);
    }

    //查询所有用户数据
    @GetMapping("/getAll")
    public Result selectAllUser(){
        List<User> userList = userService.selectAllUser();
        userList.sort(User.Comparators.ID);

        Integer code=0;
        String msg = "";
        if (userList!=null){
            code = Code.SELECT_OK;
            msg = "查询所有用户成功";
        }else{
            code = Code.SELECT_ERR;
            msg = "查询所有用户失败";
        }
        return new Result(code,msg,userList);
    }

    //查询用户数据
    @GetMapping
    public Result selectUser(HttpServletRequest req){
        User user = (User) req.getSession().getAttribute("user");

        Integer code=0;
        String msg = "";
        if (user!=null){
            code = Code.SELECT_OK;
            msg = "查询成功";
        }else{
            code = Code.SELECT_ERR;
            msg = "查询失败";
        }
        return new Result(code,msg,user);
    }

    //更新用户信息
    @PutMapping
    public Result updateInfo(@RequestBody User user,HttpServletRequest req){
        HttpSession session = req.getSession();
        User users =(User) session.getAttribute("user");
        String account = users.getAccount();
        String name = users.getName();   //旧名字
        String number = users.getNumber();      //旧电话
        String owners = user.getName();    //新名字
        String numbers = user.getNumber();  //新电话

        System.out.println("olduser: "+users);
        System.out.println("updateInfo: "+user);

        boolean flag = userService.updateUser(user);    //更改用户信息，需要更改car的owner
        boolean flags= false;

        if (name!=owners || number!=numbers){
            flags = carPlateService.updateOwner(account,owners,numbers);
        }

        Integer code=0;
        String msg = "";

        if (flag){
            session.setAttribute("user",user);
            code = Code.UPDATE_OK;
            msg = "更新信息成功";
        }else{
            code = Code.UPDATE_ERR;
            msg = "更新信息失败";
        }
        return new Result(code,msg,user);
    }

    //更新密码
    @PutMapping("/updatePwd")
    public Result updatePwd(@RequestBody User user,HttpServletRequest req){
        User users = (User) req.getSession().getAttribute("user");
        String account = users.getAccount();    //账号

        String password = user.getPassword();   //新的密码
        boolean flag = userService.updateUserPassword(account,password);
        System.out.println("updatePwd: "+account+","+password);
        Integer code=0;
        String msg = "";

        if (flag){
            code = Code.UPDATE_OK;
            msg = "更新密码成功";
        }else{
            code = Code.UPDATE_ERR;
            msg = "更新密码失败";
        }
        return new Result(code,msg,flag);
    }

    //注册
    @PostMapping("/register")
    public Result register(@RequestBody User user) throws SQLIntegrityConstraintViolationException {
        boolean flag = false;
        System.out.println("register: "+user);
        Integer code=0;
        String msg = "";

        try{
             flag = userService.addUser(user);
        }catch (Exception e) {
            throw new SQLIntegrityConstraintViolationException("账号已存在，换个试试",e);
        }

        if (flag){
            code = Code.SAVE_OK;
            msg = "注册成功";
        }else{
            code = Code.SAVE_ERR;
            msg = "注册失败";
        }
        return new Result(code,msg,flag);
    }

    //退出
    @GetMapping("/logout")
    public Result logout(HttpServletRequest req){
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        System.out.println("logout: "+user);
        boolean flag = false;
        try{
            session.invalidate();   //移除session
            flag = true;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return new Result(flag ? Code.EXIT_OK: Code.EXIT_ERR,String.valueOf(flag),user);
    }

}
